// pages/reservation/reservation.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reservationDetail: [],
    aNullShow: false,
    bNullShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getList("reservation")
      .then(res => {
        this.setData({
          reservationDetail: res.data
        })
        this.judNull()
        console.log('请求成功', res)
      })
      .catch(err => {
        console.log('请求失败', err)
      })
  },
  goToDetail(e) {
    wx.navigateTo({
      url: '../rooms/detail/detail?id=' + e.currentTarget.dataset.id
    })
  },
  //取消预约
  cancleReservation(e) {
    let that = this
    console.log("取消预约的商品id为", e.currentTarget.dataset.id)
    wx.showModal({
      content: '确定要取消预约吗？',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          // 从数据库中删除
          app.removeOne('reservation', e.currentTarget.dataset.id)
            .then(res => {
              that.data.reservationDetail.splice(e.currentTarget.dataset.index, 1)
              that.setData({
                reservationDetail: that.data.reservationDetail
              })
              that.judNull()
              console.log('取消预约成功', res)
            })
            .catch(err => {
              console.log('取消预约失败', err)
            })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //确认完成
  enterReservation(e) {
    let that = this
    console.log("确认完成的商品id为", e.currentTarget.dataset.id)
    wx.showModal({
      content: '确定要确认完成吗？',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          // 从数据库中删除
          app.db('reservation')
            .doc(e.currentTarget.dataset.id)
            .update({
              data: {
                done: true
              }
            })
            .then(res => {
              that.data.reservationDetail[e.currentTarget.dataset.index].done = true
              that.setData({
                reservationDetail: that.data.reservationDetail
              })
              that.judNull()
              console.log('确认完成成功', res)
            })
            .catch(err => {
              console.log('确认完成失败', err)
            })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  delReservation(e) {
    let that = this
    console.log("删除订单的商品id为", e.currentTarget.dataset.id)
    wx.showModal({
      content: '确定要删除订单吗？',
      confirmColor: '#ff0000',
      success(res) {
        if (res.confirm) {
          // 从数据库中删除
          app.removeOne('reservation', e.currentTarget.dataset.id)
            .then(res => {
              that.data.reservationDetail.splice(e.currentTarget.dataset.index, 1)
              that.setData({
                reservationDetail: that.data.reservationDetail
              })
              that.judNull()
              console.log('取消预约成功', res)
            })
            .catch(err => {
              console.log('取消预约失败', err)
            })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  judNull() {
    let a = 0
    let b = 0
    let list = this.data.reservationDetail
    for (let i = 0; i < list.length; i++) {
      if (!list[i].done) {
        a++;
        console.log(a);
      } else {
        b++;
        console.log(b);
      }
    }
    if (a === 0) {
      this.setData({
        aNullShow: true
      })
    } else {
      this.setData({
        aNullShow: false
      })
    }
    if (b === 0) {
      this.setData({
        bNullShow: true
      })
    } else {
      this.setData({
        bNullShow: false
      })
    }
  }
})